﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Handle.TPTest
{
    [FlowAttributes(nameof(OutfeedTensionBelt), 5)]
    public class OutfeedTensionBelt : TaskBase
    {
        #region 轴变量
        private AxisMotion _okAxis = default, _ngAxis = default;
        #endregion

        #region 上升沿
        private bool _full = false;
        #endregion

        #region 定时器
        private Ton _ton1 = new Ton();
        #endregion

        #region 点位
        #endregion

        #region 输入点
        private NormalOpen _OKNG有料信号;
        #endregion

        #region 输出点
        #endregion

        #region 报警变量
        #endregion

        #region 私有变量
        private int _ok = 0;
        private int _ng = 0;
        #endregion
        public OutfeedTensionBelt(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log)
            : base(temporary, iop, axisp, dialogHost, log)
        {
            _OKNG有料信号 = iop.GetNormalOpen("OKNG有料信号");
            _okAxis = axisp.GetAxisMotion("OK传送带");
            _ngAxis = axisp.GetAxisMotion("NG传送带");
        }

        public override void EmergencyStop()
        {

        }

        public override void Handle()
        {
            if (_temporary.SystemStatus.Auto)
            {
                if (_step == 0)
                {
                    if (_temporary.OK)
                    {
                        _ok++;
                        NextStep();
                    }
                    if (_temporary.NG)
                    {
                        _ng++;
                        NextStep(20);
                    }
                }
                if (_step == 10)
                {
                    if (_ok == 5)
                    {
                        _okAxis.Continuous(true, false);
                        NextStep(20);
                    }
                    else
                    {
                        _temporary.OK = false;
                        _step = 0;
                    }
                }
                if (_step == 20)
                {
                    if (_ng == 5)
                    {
                        _ngAxis.Continuous(true, false);
                        NextStep(20);
                    }
                    else
                    {
                        _temporary.NG = false;
                        _step = 0;
                    }
                }
                if (_step == 30)
                {
                    _ton1.Time(800, true);
                    if (_ton1.Out)
                    {
                        _okAxis.Continuous(false, false);
                        _ton1.Time(800, false);
                        _temporary.OK = false;
                        _ok = 0;
                        _step = 0;
                    }
                }
                if (_step == 40)
                {
                    _ton1.Time(800, true);
                    if (_ton1.Out)
                    {
                        _ngAxis.Continuous(false, false);
                        _ton1.Time(600, false);
                        _temporary.NG = false;
                        _ng = 0;
                        _step = 0;
                    }
                }
            }
            if (_OKNG有料信号.Execute() && !_full)
            {
                _dialogHost.DialogShow("出料皮带满料检测到有信号！！！", Core.Enum.LogLevel.Warning);
                _full = true;
            }
            if (!_OKNG有料信号.Execute() && _full)
            {
                _full = false;
            }
            _temporary.SystemStatus.AlarmStop = _temporary.SystemStatus.AlarmStop || _OKNG有料信号.Execute();
            if (_temporary.SystemStatus.ManualOperation)
            {

            }
        }

        public override void Resetting()
        {

        }

        public override void SafeStop()
        {

        }
    }
}
